import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FakeFirewall {

    private static FakeFirewall instance = null;
    private static Connect co = new Connect();

    private HashMap<String, String> coAttempt = new HashMap<String, String>();
    private List<String> localhosts = new ArrayList<String>();
    private List<Date> dateConnection = new ArrayList<Date>();
    private int cpt = 1;

    public FakeFirewall() {
    }

    public static FakeFirewall getInstance() {
        if (instance == null) {
            instance = new FakeFirewall();
        }
        return instance;
    }

    public List<String> getLocalHosts() {
        return localhosts;
    }

    public List<Date> getDateConnection() {
        return dateConnection;
    }
    
    public void addLocalHost(String localhost) {
        localhosts.add(localhost);
    }
    
    public void addDateConnection(Date date) {
        dateConnection.add(date);
    }

    public void analyze(String email, String password) {
        coAttempt.put(password, email);
        if (coAttempt.size() == 6) {
            List<String> pws = new ArrayList<String>();
            String login = "";
            for (Map.Entry<String, String> entry : coAttempt.entrySet()) {
                pws.add(entry.getKey());
                if (cpt == 1) {
                    login = entry.getValue();
                } else if (!entry.getValue().equals(login)) {
                    coAttempt.clear();
                    break;
                }
                if (cpt == 6) {
                    String pw = co.getPw(login);
                    if (!pws.contains(pw) && sameHost() && quickConnect()) {
                        co.listed(localhosts.get(0));
                        // alert(login, pws);
                    }
                    coAttempt.clear();
                    localhosts.clear();
                    dateConnection.clear();
                }
                cpt++;
            }
        }
    }

    private boolean sameHost() {
        boolean same = true;
        for (String localhost : localhosts) {
            same = localhost.equals(localhosts.get(0));
            if (same == false) break;
        }
        return same;
    }

    public boolean quickConnect() {
        boolean quick = false;
        for (Date date : dateConnection) {
            int pos = dateConnection.indexOf(date);
            Date nDate = null;
            if (pos < dateConnection.size() - 1) {
                nDate = dateConnection.get(pos + 1);
            } else {
                nDate = new Date();
            }
            long time = (nDate.getTime()/1000) - (date.getTime()/1000);
            if (time < 1) {
                quick = true;
            } else {
                quick = false;
            }
        }
        return quick;
    }

    private void alert(String login, List<String> pws) {
        System.out.println();
        System.out.println("----");
        System.out.println("Archtung : hacking versuchen !!!");
        String attempts = "";
        for (String pw : pws) {
            if (pws.indexOf(pw) < pws.size() - 1) {
                attempts += pw + ", ";
            } else {
                attempts += pw;
            }
        }
        System.out.println("Account = " + login + " - pws :" + attempts);
        System.out.println("----");
        System.out.println();
    }

}
